package com.ityj.yygh.hospital.controller.api;

import com.ityj.yygh.hospital.service.DepartmentService;
import com.ityj.yygh.hospital.service.HospitalService;
import com.ityj.yygh.hospital.service.ScheduleService;
import com.ityj.yygh.helper.HttpRequestHelper;
import com.ityj.yygh.model.hosp.Department;
import com.ityj.yygh.model.hosp.Hospital;
import com.ityj.yygh.model.hosp.Schedule;
import com.ityj.yygh.result.Result;
import com.ityj.yygh.vo.hosp.DepartmentQueryVo;
import com.ityj.yygh.vo.hosp.ScheduleQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping(path = "/api/hosp")
@Slf4j
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    // 通过hospital-manage进行医院信息(json)上传
    @PostMapping(path = "/saveHospital")
    public Result<Object> saveHospInfo(HttpServletRequest request) {
        // get data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // save data
        boolean result = hospitalService.save(stringObjectMap);
        return result ? Result.ok() : Result.fail();
    }

    @PostMapping(path = "/hospital/show")
    public Result<Object> showHospInfo(HttpServletRequest request) {
        // get input data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // get data by hoscode
        String hoscode = (String) stringObjectMap.get("hoscode");
        log.info("Hoscode = {}", hoscode);
        Hospital hospital = hospitalService.getHospitalByHoscode(hoscode);

        return Result.ok(hospital);
    }


    // 保存科室信息
    @PostMapping(path = "/saveDepartment")
    public Result<Object> saveDepartment(HttpServletRequest request) {
        // get data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // save data
        boolean result = departmentService.save(stringObjectMap);
        return result ? Result.ok() : Result.fail();
    }

    // 查询科室接口开发
    @PostMapping(path = "/department/list")
    public Result<Object> showDepartmentInfo(HttpServletRequest request) {
        // get input data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        log.info("Input parameter is : {}", stringObjectMap);
        // get data by hoscode
        String hoscode = (String) stringObjectMap.get("hoscode");
        String pageNumStr = (String) stringObjectMap.get("page");
        String pageSizeStr = (String) stringObjectMap.get("limit");
        log.info("Hoscode = {},pageNum = {},pageSize = {}", hoscode, pageNumStr, pageSizeStr);

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);

        Page<Department> pageModel = departmentService.findDepartmentByCondition(departmentQueryVo, Integer.parseInt(pageNumStr), Integer.parseInt(pageSizeStr));
        log.info("Return data size = {}", pageModel.getSize());
        return Result.ok(pageModel);
    }

    @PostMapping(path = "/department/remove")
    public Result<Object> removeDepartmentInfo(HttpServletRequest request) {
        // get input data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        log.info("Input parameter is : {}", stringObjectMap);
        String hoscode = (String) stringObjectMap.get("hoscode");
        String depcode = (String) stringObjectMap.get("depcode");

        boolean result = departmentService.remove(hoscode, depcode);
        return result ? Result.ok() : Result.fail();
    }

    // 排班的新增、查询及删除
    @PostMapping(path = "/saveSchedule")
    public Result<Object> saveSchedule(HttpServletRequest request) {
        // get data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        // save data
        boolean result = scheduleService.save(stringObjectMap);
        return result ? Result.ok() : Result.fail();
    }

    @PostMapping(path = "/schedule/list")
    public Result<Object> findSchedule(HttpServletRequest request) {
        // get input data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        log.info("Input parameter is : {}", stringObjectMap);
        // get data by hoscode
        String hoscode = (String) stringObjectMap.get("hoscode");
        String depcode = (String) stringObjectMap.get("depcode");
        String pageNumStr = (String) stringObjectMap.get("page");
        String pageSizeStr = (String) stringObjectMap.get("limit");
        log.info("Hoscode = {},pageNum = {},pageSize = {}", hoscode, pageNumStr, pageSizeStr);

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pageModel = scheduleService.findScheduleByCondition(scheduleQueryVo, Integer.parseInt(pageNumStr), Integer.parseInt(pageSizeStr));
        log.info("Return data size = {}", pageModel.getSize());
        return Result.ok(pageModel);
    }

    @PostMapping(path = "/schedule/remove")
    public Result<Object> removeSchedule(HttpServletRequest request) {
        // get input data
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);

        log.info("Input parameter is : {}", stringObjectMap);
        String hoscode = (String) stringObjectMap.get("hoscode");
        String hosScheduleId = (String) stringObjectMap.get("hosScheduleId");

        boolean result = scheduleService.remove(hoscode, hosScheduleId);
        return result ? Result.ok() : Result.fail();
    }

}
